export function formatDate(date) {
  const json_date = new Date(date).toJSON()
  return formatDateTime(new Date(new Date(json_date) + 8 * 3600 * 1000))
}

const formatDateTime = (date) => {
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? ('0' + m) : m
  let d = date.getDate()
  d = d < 10 ? ('0' + d) : d
  let h = date.getHours()
  let minute = date.getMinutes()
  minute = minute < 10 ? ('0' + minute) : minute
  let second = date.getSeconds()
  second = second < 10 ? ('0' + second) : second
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
}

export function generateRoute(menuList) {
  let routes = []
  const deepList = (list) => {
    while (list.length) {
      let item = list.shift()
      if (item.children) {
        deepList(item.children)
      } else {
        if (item.parentId[0]) {
          routes.push({
            name: item.component,
            path: item.path,
            meta: {
              title: item.menuName
            },
            component: item.componentPath
          })
        }
      }
    }
  }
  deepList(menuList)
  return routes
}
